package com.ccjk.beusoft.plugin.host

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.AssetManager
import android.content.res.Resources
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.*
import com.ccjk.beusoft.hot.plugin.Const
import com.ccjk.beusoft.hot.plugin.PluginActivityInterface
import com.ccjk.beusoft.hot.plugin.PluginPackage

/**
 * Created by CCJK on 2017/12/2.
 */
abstract class BasePluginActivity : AppCompatActivity(), PluginActivityInterface {
    lateinit var proxyActivity: Activity
    private lateinit var pluginPackage: PluginPackage
    private var from: Int = Const.FROM_INTERNAL

    override fun bind(activity: Activity, pluginPackage: PluginPackage) {
        this.proxyActivity = activity
        this.pluginPackage = pluginPackage

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            from = savedInstanceState.getInt(Const.KEY_ACTIVITY_FROM, Const.FROM_INTERNAL)
        }
        if (from == Const.FROM_INTERNAL) {
            super.onCreate(savedInstanceState)
            proxyActivity = this
        }

    }

    override fun setContentView(layoutResID: Int) {
        if (from == Const.FROM_INTERNAL)
            super.setContentView(layoutResID)
        else proxyActivity.setContentView(layoutResID)
    }

    override fun setContentView(view: View?) {
        if (from == Const.FROM_INTERNAL) super.setContentView(view)
        else proxyActivity.setContentView(view)
    }

    override fun setContentView(view: View?, params: ViewGroup.LayoutParams?) {
        if (from == Const.FROM_INTERNAL) super.setContentView(view, params)
        else proxyActivity.setContentView(view, params)
    }

    override fun addContentView(view: View?, params: ViewGroup.LayoutParams?) {
        if (from == Const.FROM_INTERNAL) super.addContentView(view, params)
        else proxyActivity.addContentView(view, params)
    }

    override fun <T : View?> findViewById(id: Int): T =
            if (from == Const.FROM_INTERNAL) super.findViewById<T>(id)
            else proxyActivity.findViewById<T>(id)

    override fun getIntent(): Intent =
            if (from == Const.FROM_INTERNAL) super.getIntent()
            else proxyActivity.intent

    override fun getClassLoader(): ClassLoader =
            if (from == Const.FROM_INTERNAL)
                super.getClassLoader()
            else proxyActivity.classLoader

    override fun getResources(): Resources =
            if (from == Const.FROM_INTERNAL) super.getResources()
            else proxyActivity.resources

    override fun getAssets(): AssetManager =
            if (from == Const.FROM_INTERNAL) super.getAssets()
            else proxyActivity.assets


    override fun getPackageName(): String =
            if (from == Const.FROM_INTERNAL) super.getPackageName()
            else proxyActivity.packageName

    override fun getLayoutInflater(): LayoutInflater =
            if (from == Const.FROM_INTERNAL) super.getLayoutInflater()
            else proxyActivity.layoutInflater

    override fun getMenuInflater(): MenuInflater =
            if (from == Const.FROM_INTERNAL) super.getMenuInflater()
            else proxyActivity.menuInflater

    override fun getSharedPreferences(name: String?, mode: Int): SharedPreferences =
            if (from == Const.FROM_INTERNAL) super.getSharedPreferences(name, mode)
            else proxyActivity.getSharedPreferences(name, mode)

    override fun getApplicationContext(): Context =
            if (from == Const.FROM_INTERNAL) super.getApplicationContext()
            else proxyActivity.applicationContext

    override fun getWindowManager(): WindowManager =
            if (from == Const.FROM_INTERNAL) super.getWindowManager()
            else proxyActivity.windowManager

    override fun getWindow(): Window =
            if (from == Const.FROM_INTERNAL) super.getWindow()
            else proxyActivity.window


    override fun getSystemService(name: String?): Any =
            if (from == Const.FROM_INTERNAL) super.getSystemService(name)
            else proxyActivity.getSystemService(name)


    override fun onStart() {
        if (from == Const.FROM_INTERNAL) super.onStart()
    }

    override fun onRestart() {
        if (from == Const.FROM_INTERNAL) super.onRestart()

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (from == Const.FROM_INTERNAL) super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onResume() {
        if (from == Const.FROM_INTERNAL) super.onResume()
    }

    override fun onPause() {
        if (from == Const.FROM_INTERNAL) super.onPause()
    }

    override fun onStop() {
        if (from == Const.FROM_INTERNAL) super.onStop()
    }

    override fun onDestroy() {
        if (from == Const.FROM_INTERNAL) super.onDestroy()
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        if (from == Const.FROM_INTERNAL) super.onSaveInstanceState(outState)
    }

    override fun onNewIntent(intent: Intent?) {
        if (from == Const.FROM_INTERNAL) super.onNewIntent(intent)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        if (from == Const.FROM_INTERNAL) super.onRestoreInstanceState(savedInstanceState)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean =
            if (from == Const.FROM_INTERNAL) super.onTouchEvent(event)
            else false


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean =
            if (from == Const.FROM_INTERNAL) super.onKeyUp(keyCode, event) else false

    override fun onWindowAttributesChanged(params: WindowManager.LayoutParams?) {
        if (from == Const.FROM_INTERNAL) super.onWindowAttributesChanged(params)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        if (from == Const.FROM_INTERNAL) super.onWindowFocusChanged(hasFocus)
    }

    override fun onBackPressed() {
        if (from == Const.FROM_INTERNAL) super.onBackPressed()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean =
            if (from == Const.FROM_INTERNAL) super.onCreateOptionsMenu(menu) else false


    override fun onOptionsItemSelected(item: MenuItem?): Boolean =
            if (from == Const.FROM_INTERNAL) super.onOptionsItemSelected(item) else false


    override fun finish() {
        if (from == Const.FROM_INTERNAL) super.finish()
        else proxyActivity.finish()
    }

}